package com.haierCamera.customapplication.ui;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklActivityWebViewBinding;
import com.haierCamera.customapplication.di.TokenManager;

import javax.inject.Inject;

/**
 * Created by AdministratorZJ
 * on 2019/10/18.
 */
public class WebViewPublicActivity extends BaseActivity {
    private HzklActivityWebViewBinding webViewBinding;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private String id;
    @Inject
    TokenManager tokenManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webViewBinding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_web_view);
        initData();
    }

    private void initData() {
        //getPermission();
        webViewBinding.registerBack.setOnClickListener(v -> finish());
        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");
        webViewBinding.title.setText(title);
        Log.i("TAG","url-" + url);
        WebSettings settings = webViewBinding.webView.getSettings();
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(false);
        settings.setAllowUniversalAccessFromFileURLs(false);
        // 禁止 file 协议加载 JavaScript
        if (url.startsWith("file://")) {
            settings.setJavaScriptEnabled(false);
        } else {
            settings.setJavaScriptEnabled(true);
        }
        settings.setAllowFileAccess(true);
        settings.setDomStorageEnabled(true);//这设置存储 不设置显示不出来
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没有网使用缓存
        settings.setUseWideViewPort(true);//设置视图可缩放和下面一起设置 似乎没效果
        settings.setLoadWithOverviewMode(true);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            settings.setAllowFileAccessFromFileURLs(true);
            settings.setAllowUniversalAccessFromFileURLs(true);
        }
        webViewBinding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        webViewBinding.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    webViewBinding.progressBar.setVisibility(View.GONE);
                } else {
                    webViewBinding.progressBar.setVisibility(View.VISIBLE);
                }
            }
        });
        webViewBinding.webView.loadUrl(url);
    }

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    private void getPermission() {
        int hasWriteContactsPermission = ContextCompat.checkSelfPermission(WebViewPublicActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(WebViewPublicActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(WebViewPublicActivity.this,
                        PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }

            ActivityCompat.requestPermissions(WebViewPublicActivity.this,
                    PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }

        while ((ContextCompat.checkSelfPermission(WebViewPublicActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) != PackageManager.PERMISSION_GRANTED) {
        }
    }

}
